﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;

namespace KD.Sms.DAL.SqlLite
{
    internal sealed partial class Main
    {
        /// <summary>
        /// 获取用于弃料提示的判断
        /// </summary>
        /// <returns></returns>
        public DataTable DoQueryWasteWaringJudgeParms()
        {
            return SQLiteHelper.GetDataTable(@"select a.Param_Value as WasteCurrent,b.Param_Value as WasteCount from SysParam  a
                                                left join SysParam b
                                                on 1 = 1 and b.Param_Name = 'WasteCount'
                                                where a.Param_Name = 'WasteCurrent'");
        }

        /// <summary>
        /// 标记弃料完成
        /// </summary>
        /// <returns></returns>
        public bool DoUpdateWasteComplete()
        {
            return SQLiteHelper.ExecuteNonQuery("update SysParam set Param_Value = '0' where Param_Name = 'WasteCurrent'", null) > 0;
        }

        /// <summary>
        /// 获取用于重量报警的阈值
        /// </summary>
        /// <returns></returns>
        public DataTable DoQueryWaringParmsForSampleWeight()
        {
            return SQLiteHelper.GetDataTable(@"select a.Param_Value as MaxWeight,
                                                           b.Param_Value as MinWeight,
                                                           c.Param_Value as MemoryReductionc,
                                                           d.Param_Value as DefaultReductionc
                                                      from SysParam a
                                                      left join SysParam b
                                                        on 1 = 1
                                                       and b.Param_Name = 'MinWeight'
                                                      left join SysParam c
                                                        on 1 = 1
                                                       and c.Param_Name = 'MemoryReductionc'
                                                      left join SysParam d
                                                        on 1 = 1
                                                       and d.Param_Name = 'DefaultReductionc'
                                                     where a.Param_Name = 'MaxWeight'");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable DoQueryParmsForEdit()
        {
            return SQLiteHelper.GetDataTable("select Param_Name,Param_Value,Param_Desc from SysParam  order by rowid");
        }

        /// <summary>
        /// 保存参数设置
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public bool DoSaveParms(DataGridView grid)
        {
            try
            {
                List<string> list = new List<string>();
                foreach (DataGridViewRow row in grid.Rows)
                {
                    if (row.Index >= 0)
                    {
                        list.Add(string.Format("update SysParam set Param_Desc = '{0}',Param_Value = '{1}' where Param_Name = '{2}'"
                            , row.Cells["Param_Desc"].Value.ToString(), row.Cells["Param_Value"].Value.ToString(), row.Cells["Param_Name"].Value.ToString()));
                    }
                }
                SQLiteHelper.ExecSQLTrans(list);
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Sqlite, "DoCleanDataBase()");
                Log.LogHelper.DoWriteLogTypeSubitem(Log.LogType.Error, Log.LogSubitem.Sqlite, ex.ToString());
                return false;
            }
        }

    }
}
